/******************************************************************************
 * Copyright (C) 2015  671643387@qq.com
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the
 * Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
 * more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 *
 *****************************************************************************/

#ifndef NODE_UTIL_LOCAL_BYTEBUFFER_H_
#define NODE_UTIL_LOCAL_BYTEBUFFER_H_

#if defined(_MSC_VER) && (_MSC_VER >= 1200)
# pragma once
#endif

#include <cstddef>
#include <cstdint>
#include <vector>

#include "node/macros.h"
#include "node/util/bytebuffer_exception.h"

namespace node {
    namespace util {

        template<::std::size_t SIZE>
        class LocalByteBuffer
        {
        public:
            typedef LocalByteBuffer  Self;

        public:
            LocalByteBuffer(void)
                : rpos_(0)
                , wpos_(0)
            {
            }

            void Clear()
            {
                rpos_ = wpos_ = 0;
            }

            ~LocalByteBuffer(void)
            {

            }

        public:
            ::std::size_t size(void) const
            {
                return wpos_;
            }

            const ::std::uint8_t* contents(void) const
            {
                return &storage_[0];
            }

            Self& operator<<(::std::uint8_t x)
            {
                Append<::std::uint8_t>(x);
                return *this;
            }

            Self& operator<<(::std::uint16_t x)
            {
                Append<::std::uint16_t>(x);
                return *this;
            }

            Self& operator<<(::std::uint32_t x)
            {
                Append<::std::uint32_t>(x);
                return *this;
            }

            Self& operator<<(::std::uint64_t x)
            {
                Append<::std::uint64_t>(x);
                return *this;
            }

            Self& operator<<(float x)
            {
                Append(x);
                return *this;
            }

            Self& operator<<(const char* x)
            {
                Append(x, strlen(x));
                *this << (::std::uint8_t)0;
                return *this;
            }

            Self& operator<<(const ::std::string& x)
            {
                Append(x.c_str(), x.length());
                *this << (::std::uint8_t)0;
                return *this;
            }

            void Append(const char* src, ::std::size_t len)
            {
                if (len == 0)
                    return;

                if ((len + wpos_) > SIZE)
                    throw ::node::util::ByteBufferException(true, 
                        wpos_, SIZE, len);

                memcpy(&storage_[wpos_], src, len);
                wpos_ += len;
            }

            void Append(const ::std::uint8_t* src, ::std::size_t len)
            {
                Append((const char*)src, len);
            }

            void Append(const LocalByteBuffer& bbuf)
            {
                Append(bbuf.contents(), bbuf.size());
            }

        protected:
            template<typename T>
            void Append(T x)
            {
                Append((const char*)&x, sizeof(T));
            }

        protected:
            ::std::size_t                 rpos_;
            ::std::size_t                 wpos_;
            ::std::uint8_t                storage_[SIZE];

            DISALLOW_COPY_AND_ASSIGN(LocalByteBuffer);
        };

    }
}

#endif // NODE_UTIL_LOCAL_BYTEBUFFER_H_
